package com.blen.videolib;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.blen.videolibs.R;


/**
 * Created by Blensmile on 2016/7/29.
 */
public class ProgressDialog extends AlertDialog {


    public static final int CIRCLE = 0;
    public static final int FLOWER = 1;
    private ImageView imgCircle;
    private Animation animation;
    private TextView tvMsg;
    private View llWraper;
    private String msg;
    private boolean cancelable = true;
    private int type = 0;
    private Context context;

    public ProgressDialog(Context context) {
        super(context, com.blen.videolibs.R.style.dialog);
        this.context = context;
    }

    public ProgressDialog(Context context, int type){
        this(context);
        this.type = type;
    }

    public ProgressDialog(Context context, String msg){
        this(context);
        this.msg = msg;
    }

    public ProgressDialog(Context context, String msg, int type){
        this(context);
        this.msg = msg;
        this.type = type;
    }

    public void setMsg(String msg){
        //线程安全问题..
        if(tvMsg != null){
            this.msg = msg;
            tvMsg.setText(msg);
        }
    }

    public TextView getTvMsg(){
        return tvMsg;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.blen.videolibs.R.layout.circle_progress_dialog);
        setCanceledOnTouchOutside(false);
        animation = AnimationUtils.loadAnimation(getContext(), com.blen.videolibs.R.anim.progress_dialog_rotate);
        animation.setInterpolator(new LinearInterpolator());
        animation.setFillAfter(true);
        imgCircle = (ImageView) findViewById(com.blen.videolibs.R.id.img_circle);
        switch (type){
            default:
            case CIRCLE:
            case FLOWER:
                animation.setDuration(1500);
                break;
        }
        tvMsg = (TextView) findViewById(com.blen.videolibs.R.id.tv_msg);
        llWraper = findViewById(R.id.ll_wraper);
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        if(!TextUtils.isEmpty(msg)) {
            tvMsg.setVisibility(View.VISIBLE);
            tvMsg.setText(msg);
        }else {
            tvMsg.setVisibility(View.GONE);
            ViewGroup.LayoutParams params = llWraper.getLayoutParams();
            params.width = params.height;
        }
    }

    /**
     * 在AlertDialog的 onStart() 生命周期里面执行开始动画
     */

    @Override
    protected void onStart() {
        super.onStart();
        if (animation != null) {
            imgCircle.startAnimation(animation);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        imgCircle.clearAnimation();
    }

}
